<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\model;

use think\Model;
use think\Db;
class ArticleModel extends Model
{
    // 确定链接表名
    protected $table = 'wk_articles';

    /**
     * 查询文章
     * @param $where
     * @param $offset
     * @param $limit
     */
    public function getArticlesByWhere($where, $offset, $limit)
    {
       return Db::field('a.*,c.cate_name')
            ->table(['wk_articles'=>'a','wk_category'=>'c'])
            ->where('a.cid = c.id')
            ->where($where)
            ->limit($offset,$limit)
            ->order('id desc')
            ->select();
    }

    /**
     * 根据搜索条件获取所有的文章数量
     * @param $where
     */
    public function getAllArticles($where)
    {
        return $this->where($where)->count();
    }

    /**
     * 添加文章
     * @param $param
     */
    public function addArticle($param)
    {
        try{
            $result = $this->validate('ArticleValidate')->save($param);
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{

                return msg(1, url('admin/Articles/index'), '添加文章成功');
            }
        }catch (\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 编辑文章信息
     * @param $param
     */
    public function editArticle($param)
    {
        try{
            $result = $this->validate('ArticleValidate')->save($param, ['id' => $param['id']]);
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{

                return msg(1, url('admin/Articles/index'), '编辑文章成功');
            }
        }catch(\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 根据文章的id 获取文章的信息
     * @param $id
     */
    public function getOneArticle($id)
    {
        return Db::field('a.id,a.title,a.description,a.keywords,a.thumbnail,a.content,a.add_time,c.cate_name,a.cid')
            ->table(['wk_articles'=>'a','wk_category'=>'c'])
            ->where('a.cid = c.id')
            ->where('a.id',$id)
            ->find();
//        return $this->where('id', $id)->find();
    }


    /**
     * 删除文章
     * @param $id
     */
    public function delArticle($id)
    {
        try{

            $this->where('id', $id)->delete();
            $img=$this->field('thumbnail')->where('id',$id)->find();
//            return msg(1, '', $img['thumbnail']);
//            if ($img){
//                unlink('public'.$img);
//                return msg(1, '', '123123213');
//
//            }
            return msg(1, '', '删除文章成功');

        }catch(\Exception $e){
            return msg(-1, '', $e->getMessage());
        }
    }
}